package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.imgedit.utils.Matrix3;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.bean.ImagePiece;
import flc.ast.databinding.ActivityPicGridBinding;
import flc.ast.utils.Content;
import flc.ast.utils.ImageSplitter;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class PicGridActivity extends BaseAc<ActivityPicGridBinding> {

    public static String imgPath = "";
    private Bitmap mImgBitmap;
    private List<ImagePiece> pictureSplitList = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_grid;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPicGridBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvPicGridSave.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvPicGridSave://保存
                saveImg();
                break;
        }
    }

    @Override
    protected void initData() {
        if (imgPath == null || TextUtils.isEmpty(imgPath)) return;
        setImg();
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.handling));
        RxUtil.create(new RxUtil.Callback<Bitmap>() {
            @Override
            public void doBackground(ObservableEmitter<Bitmap> emitter) {
                RectF cropRect = mDataBinding.cropImgView.getCropRect();// 剪切区域矩形
                Matrix touchMatrix = mDataBinding.ivPicGridImg.getImageViewMatrix();
                float[] data = new float[9];
                touchMatrix.getValues(data);// 底部图片变化记录矩阵原始数据
                Matrix3 cal = new Matrix3(data);// 辅助矩阵计算类
                Matrix3 inverseMatrix = cal.inverseMatrix();// 计算逆矩阵
                Matrix m = new Matrix();
                m.setValues(inverseMatrix.getValues());
                m.mapRect(cropRect);// 变化剪切矩形

                Bitmap resultBit = Bitmap.createBitmap(mImgBitmap,
                        (int) cropRect.left, (int) cropRect.top,
                        (int) cropRect.width(), (int) cropRect.height());
                emitter.onNext(resultBit);
            }

            @Override
            public void accept(Bitmap bitmap) {
                List<String> list = new ArrayList<>();
                if (bitmap != null) {
                    pictureSplitList = ImageSplitter.split(bitmap, 3, 3);
                    for (ImagePiece imagePiece : pictureSplitList) {
                        //保存到相册
                        ImageUtils.save2Album(imagePiece.bitmap, Bitmap.CompressFormat.PNG);
                        String resultFile = FileUtil.generateFilePath(Content.myCreate, ".png");
                        ImageUtils.save(imagePiece.bitmap, resultFile, Bitmap.CompressFormat.PNG);
                        list.add(resultFile);
                    }
                }
                dismissDialog();
                PicGridResultActivity.listPath = list;
                startActivity(PicGridResultActivity.class);
            }
        });
    }

    //设置图片
    private void setImg() {
        mImgBitmap = ImageUtils.getBitmap(imgPath);
        mDataBinding.ivPicGridImg.setImageBitmap(mImgBitmap);
        mDataBinding.ivPicGridImg.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mDataBinding.ivPicGridImg.setScaleEnabled(false);// 禁用缩放
        mDataBinding.ivPicGridImg.post(new Runnable() {
            @Override
            public void run() {
                RectF bitmapRect = mDataBinding.ivPicGridImg.getBitmapRect();
                mDataBinding.cropImgView.setCropRect(bitmapRect);
            }
        });
    }

}